package com.foreveross.util.cfg;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.Properties;

/**
 * 本地加载.properties文件抽象
 */
public abstract class PropertiesConfigLocalLoader implements ConfigLocalLoader {

    protected static Logger logger = LoggerFactory.getLogger(PropertiesConfigLocalLoader.class);

    private boolean hasLoad = false;

    private Properties prop = null;

    public PropertiesConfigLocalLoader() {
    }

    public Properties getProp() {
        return prop;
    }

    public void setProp(Properties prop) {
        this.prop = prop;
    }

    public boolean isHasLoad() {
        return hasLoad;
    }

    public void setHasLoad(boolean hasLoad) {
        this.hasLoad = hasLoad;
    }

    public void load() {
        prop = null;
        try {
            prop = new Properties();
            prop.load(PropertiesConfigLocalLoader.class.getClassLoader().getResourceAsStream(this.getFileName()));
        } catch (IOException e) {
            e.printStackTrace();
            logger.error(String.format("加载配件文件[%s]出错!", this.getFileName()), e);
        }
        if (prop != null) ConfigContainer.getInstance().registerCache(this.getFileName(), prop);

        this.setHasLoad(true);
    }

}
